package com.ming.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.yygh.cmn.client.DictFeignClient;
import com.ming.yygh.common.result.Result;
import com.ming.yygh.enums.DictEnum;
import com.ming.yygh.model.user.Patient;
import com.ming.yygh.user.mapper.PatientMapper;
import com.ming.yygh.user.service.PatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Light
 * @date 2021/12/21 11:18
 */
@Service
public class PatientServiceImpl extends
        ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllByUserId(String userId) {
        // 根据userId查询所有就诊人信息列表
        List<Patient> patientList = baseMapper.selectList(new QueryWrapper<Patient>().eq("user_id", userId));
        // 通过远程调用，查询数据字典表，得到编码对应的具体内容
        // 其他参数的封装
        patientList.forEach(this::packPatient);

        return patientList;
    }

    @Override
    public Patient getPatientById(String id) {
        return this.packPatient(baseMapper.selectById(id));
    }

    /**
     * patient对象中，其他参数的封装
     *
     * @param patient
     */
    private Patient packPatient(Patient patient) {
        // 根据证件编码，获取证件类型具体值
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType()).getData();
        // 联系人证件类型
        String contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType()).getData();
        // 省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode()).getData();
        // 市
        String cityString = dictFeignClient.getName(patient.getCityCode()).getData();
        // 区
        String districtString = dictFeignClient.getName(patient.getDistrictCode()).getData();

        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
